﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Maticsoft.DBUtility;

namespace ElingOA6_Admin_DSJCJ.DAL
{
    /// <summary>
    /// 组织机构BLL
    /// </summary>
    public class Entity
    {
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="fEntityNo">组织机构代码</param>
        /// <returns>返回组织机构信息</returns>
        public Model.Entity GetModel(string fEntityNo)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FEntityNo,FEntityName,FLeader,FShortName,FAccessDate,FLastLoginIPAddr,FLogID,fuseTimes,FPublishRoot,FWaterMarkContent,FWaterMarkSvrFileName,FWaterMarkExt,FWaterMarkOldFileName,FWebBeiAnNo,FCopyRight,FTxtWaterMark,FSmtpHost,FSmtpAccount,FSmtpPassword,FWaterMarkCls,FSmtpPort,FIsSsl,FFlowLockTime,FIsIPControl,FNTKOCaption,FNTKOKey,FSysCopyRight,FSysTitle,FFtpShowText from t_Entity ");
            strSql.Append(" where FEntityNo=@FEntityNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)			};
            parameters[0].Value = fEntityNo;

            Model.Entity model = new Model.Entity();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["FEntityNo"] != null && ds.Tables[0].Rows[0]["FEntityNo"].ToString() != "")
                {
                    model.FEntityNo = ds.Tables[0].Rows[0]["FEntityNo"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FEntityName"] != null && ds.Tables[0].Rows[0]["FEntityName"].ToString() != "")
                {
                    model.FEntityName = ds.Tables[0].Rows[0]["FEntityName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FLeader"] != null && ds.Tables[0].Rows[0]["FLeader"].ToString() != "")
                {
                    model.FLeader = ds.Tables[0].Rows[0]["FLeader"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FShortName"] != null && ds.Tables[0].Rows[0]["FShortName"].ToString() != "")
                {
                    model.FShortName = ds.Tables[0].Rows[0]["FShortName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FAccessDate"] != null && ds.Tables[0].Rows[0]["FAccessDate"].ToString() != "")
                {
                    model.FAccessDate = DateTime.Parse(ds.Tables[0].Rows[0]["FAccessDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["FLastLoginIPAddr"] != null && ds.Tables[0].Rows[0]["FLastLoginIPAddr"].ToString() != "")
                {
                    model.FLastLoginIPAddr = ds.Tables[0].Rows[0]["FLastLoginIPAddr"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FLogID"] != null && ds.Tables[0].Rows[0]["FLogID"].ToString() != "")
                {
                    model.FLogID = ds.Tables[0].Rows[0]["FLogID"].ToString();
                }
                if (ds.Tables[0].Rows[0]["fuseTimes"] != null && ds.Tables[0].Rows[0]["fuseTimes"].ToString() != "")
                {
                    model.fuseTimes = int.Parse(ds.Tables[0].Rows[0]["fuseTimes"].ToString());
                }
                if (ds.Tables[0].Rows[0]["FPublishRoot"] != null && ds.Tables[0].Rows[0]["FPublishRoot"].ToString() != "")
                {
                    model.FPublishRoot = ds.Tables[0].Rows[0]["FPublishRoot"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FWaterMarkContent"] != null && ds.Tables[0].Rows[0]["FWaterMarkContent"].ToString() != "")
                {
                    model.FWaterMarkContent = (byte[])ds.Tables[0].Rows[0]["FWaterMarkContent"];
                }
                if (ds.Tables[0].Rows[0]["FWaterMarkSvrFileName"] != null && ds.Tables[0].Rows[0]["FWaterMarkSvrFileName"].ToString() != "")
                {
                    model.FWaterMarkSvrFileName = ds.Tables[0].Rows[0]["FWaterMarkSvrFileName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FWaterMarkExt"] != null && ds.Tables[0].Rows[0]["FWaterMarkExt"].ToString() != "")
                {
                    model.FWaterMarkExt = ds.Tables[0].Rows[0]["FWaterMarkExt"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FWaterMarkOldFileName"] != null && ds.Tables[0].Rows[0]["FWaterMarkOldFileName"].ToString() != "")
                {
                    model.FWaterMarkOldFileName = ds.Tables[0].Rows[0]["FWaterMarkOldFileName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FWebBeiAnNo"] != null && ds.Tables[0].Rows[0]["FWebBeiAnNo"].ToString() != "")
                {
                    model.FWebBeiAnNo = ds.Tables[0].Rows[0]["FWebBeiAnNo"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FCopyRight"] != null && ds.Tables[0].Rows[0]["FCopyRight"].ToString() != "")
                {
                    model.FCopyRight = ds.Tables[0].Rows[0]["FCopyRight"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FTxtWaterMark"] != null && ds.Tables[0].Rows[0]["FTxtWaterMark"].ToString() != "")
                {
                    model.FTxtWaterMark = ds.Tables[0].Rows[0]["FTxtWaterMark"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FSmtpHost"] != null && ds.Tables[0].Rows[0]["FSmtpHost"].ToString() != "")
                {
                    model.FSmtpHost = ds.Tables[0].Rows[0]["FSmtpHost"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FSmtpAccount"] != null && ds.Tables[0].Rows[0]["FSmtpAccount"].ToString() != "")
                {
                    model.FSmtpAccount = ds.Tables[0].Rows[0]["FSmtpAccount"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FSmtpPassword"] != null && ds.Tables[0].Rows[0]["FSmtpPassword"].ToString() != "")
                {
                    model.FSmtpPassword = ds.Tables[0].Rows[0]["FSmtpPassword"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FWaterMarkCls"] != null && ds.Tables[0].Rows[0]["FWaterMarkCls"].ToString() != "")
                {
                    model.FWaterMarkCls = ds.Tables[0].Rows[0]["FWaterMarkCls"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FSmtpPort"] != null && ds.Tables[0].Rows[0]["FSmtpPort"].ToString() != "")
                {
                    model.FSmtpPort = int.Parse(ds.Tables[0].Rows[0]["FSmtpPort"].ToString());
                }
                if (ds.Tables[0].Rows[0]["FIsSsl"] != null && ds.Tables[0].Rows[0]["FIsSsl"].ToString() != "")
                {
                    model.FIsSsl = decimal.Parse(ds.Tables[0].Rows[0]["FIsSsl"].ToString());
                }
                if (ds.Tables[0].Rows[0]["FFlowLockTime"] != null && ds.Tables[0].Rows[0]["FFlowLockTime"].ToString() != "")
                {
                    model.FFlowLockTime = int.Parse(ds.Tables[0].Rows[0]["FFlowLockTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["FIsIPControl"] != null && ds.Tables[0].Rows[0]["FIsIPControl"].ToString() != "")
                {
                    model.FIsIPControl = ds.Tables[0].Rows[0]["FIsIPControl"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FNTKOCaption"] != null && ds.Tables[0].Rows[0]["FNTKOCaption"].ToString() != "")
                {
                    model.FNTKOCaption = ds.Tables[0].Rows[0]["FNTKOCaption"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FNTKOKey"] != null && ds.Tables[0].Rows[0]["FNTKOKey"].ToString() != "")
                {
                    model.FNTKOKey = ds.Tables[0].Rows[0]["FNTKOKey"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FSysCopyRight"] != null && ds.Tables[0].Rows[0]["FSysCopyRight"].ToString() != "")
                {
                    model.FSysCopyRight = ds.Tables[0].Rows[0]["FSysCopyRight"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FSysTitle"] != null && ds.Tables[0].Rows[0]["FSysTitle"].ToString() != "")
                {
                    model.FSysTitle = ds.Tables[0].Rows[0]["FSysTitle"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FFtpShowText"] != null && ds.Tables[0].Rows[0]["FFtpShowText"].ToString() != "")
                {
                    model.FFtpShowText = ds.Tables[0].Rows[0]["FFtpShowText"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        ///更新系统水印文件相关信息
        /// <creator>zhongyl</creator>
        /// <date>2013-7-13</date>
        /// </summary>
        /// <param name="entityNo">组织机构编号</param>
        /// <param name="fWaterMarkContent">图片水印文件内容</param>
        /// <param name="fWaterMarkSvrFileName">图片水印文件服务器路径</param>
        /// <param name="fWaterMarkExt">图片文件文件类型</param>
        /// <param name="fWaterMarkOldFileName">图片水印文件原文件名</param>
        public void UpdateEntityWaterFile(string entityNo, byte[] fWaterMarkContent, string fWaterMarkSvrFileName,
            string fWaterMarkExt, string fWaterMarkOldFileName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_Entity set ");
            strSql.Append("FWaterMarkContent=@FWaterMarkContent,");
            strSql.Append("FWaterMarkSvrFileName=@FWaterMarkSvrFileName,");
            strSql.Append("FWaterMarkExt=@FWaterMarkExt,");
            strSql.Append("FWaterMarkOldFileName=@FWaterMarkOldFileName");
            strSql.Append(" where FEntityNo=@FEntityNo");
            SqlParameter[] parameters = {
				    new SqlParameter("@FEntityNo", SqlDbType.VarChar,12),
					new SqlParameter("@FWaterMarkContent", SqlDbType.Image),
					new SqlParameter("@FWaterMarkSvrFileName", SqlDbType.VarChar,255),
					new SqlParameter("@FWaterMarkExt", SqlDbType.VarChar,80),
					new SqlParameter("@FWaterMarkOldFileName", SqlDbType.VarChar,255)};
            parameters[0].Value = entityNo;
            parameters[1].Value = fWaterMarkContent;
            parameters[2].Value = fWaterMarkSvrFileName;
            parameters[3].Value = fWaterMarkExt;
            parameters[4].Value = fWaterMarkOldFileName;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);

        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FEntityNo,FEntityName,FLeader,FShortName,FAccessDate,FLastLoginIPAddr,FLogID,fuseTimes,FPublishRoot,FWaterMarkContent,FWaterMarkSvrFileName,FWaterMarkExt,FWaterMarkOldFileName,FWebBeiAnNo,FCopyRight,FTxtWaterMark,FSmtpHost,FSmtpAccount,FSmtpPassword,FWaterMarkCls,FSmtpPort,FIsSsl,FFlowLockTime,FIsIPControl,FNTKOCaption,FNTKOKey,FSysCopyRight,FSysTitle,FFtpShowText ");
            strSql.Append(" FROM t_Entity ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 更新相关数据
        /// </summary>
        /// <param name="entityNo">组织机构编号</param>
        /// <param name="entityModel">需变更组织机构信息</param>
        public void UpdateEntity(string entityNo, Model.Entity entityModel)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_Entity set ");
            strSql.Append("FEntityName=@FEntityName,");
            strSql.Append("FLeader=@FLeader,");
            strSql.Append("FShortName=@FShortName,");
            strSql.Append("fuseTimes=fuseTimes+1,");
            strSql.Append("FCopyRight=@FCopyRight,");
            strSql.Append("FTxtWaterMark=@FTxtWaterMark,");
            strSql.Append("FWaterMarkCls=@FWaterMarkCls,");
            strSql.Append("FSmtpHost=@FSmtpHost,");
            strSql.Append("FSmtpAccount=@FSmtpAccount,");
            strSql.Append("FSmtpPassword=@FSmtpPassword,");
            strSql.Append("FSmtpPort=@FSmtpPort,");
            strSql.Append("FIsSsl=@FIsSsl,");
            strSql.Append("FSysTitle=@FSysTitle,");
            strSql.Append("FFtpShowText=@FFtpShowText");
            strSql.Append(" where FEntityNo=@FEntityNo");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.VarChar,12),
					new SqlParameter("@FEntityName", SqlDbType.VarChar,50),
					new SqlParameter("@FLeader", SqlDbType.VarChar,10),
					new SqlParameter("@FShortName", SqlDbType.VarChar,20),
					new SqlParameter("@FCopyRight", SqlDbType.Text),
					new SqlParameter("@FTxtWaterMark", SqlDbType.VarChar,20),
					new SqlParameter("@FWaterMarkCls", SqlDbType.Char,1),
					new SqlParameter("@FSmtpHost", SqlDbType.VarChar,80),
					new SqlParameter("@FSmtpAccount", SqlDbType.VarChar,80),
					new SqlParameter("@FSmtpPassword", SqlDbType.VarChar,30),
                    new SqlParameter("@FSmtpPort", SqlDbType.Int,4),
					new SqlParameter("@FIsSsl", SqlDbType.Decimal,5),
                    new SqlParameter("@FSysTitle",SqlDbType.NVarChar,200),
                                        new SqlParameter("@FFtpShowText",SqlDbType.NVarChar,5000)};
            parameters[0].Value = entityNo;
            parameters[1].Value = entityModel.FEntityName;
            parameters[2].Value = entityModel.FLeader;
            parameters[3].Value = entityModel.FShortName;
            parameters[4].Value = entityModel.FCopyRight;
            parameters[5].Value = entityModel.FTxtWaterMark;
            parameters[6].Value = entityModel.FWaterMarkCls;
            parameters[7].Value = entityModel.FSmtpHost;
            parameters[8].Value = entityModel.FSmtpAccount;
            parameters[9].Value = entityModel.FSmtpPassword;
            parameters[10].Value = entityModel.FSmtpPort;
            parameters[11].Value = entityModel.FIsSsl;
            parameters[12].Value = entityModel.FSysTitle;
            parameters[13].Value = entityModel.FFtpShowText;
            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


    }
}
